package palmtop.com.palmtop.activity;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.os.PowerManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.ToggleButton;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;

import net.grandcentrix.tray.AppPreferences;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.event.MainSyncEvent;
import palmtop.com.palmtop.fragment.MainFragment;
import palmtop.com.palmtop.fragment.MyFragment;
import palmtop.com.palmtop.fragment.ReportFragment;
import palmtop.com.palmtop.fragment.TaskFragment;
import palmtop.com.palmtop.service.NotificationService;
import palmtop.com.palmtop.service.NotifyService;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.ToastUtil;

public class Main2Activity extends BaseActivity implements TaskFragment.OnFragmentInteractionListener, ReportFragment.OnFragmentInteractionListener, MyFragment.OnFragmentInteractionListener {

    @BindView(R.id.contents)
    FrameLayout contents;
    @BindView(R.id.btnMain)
    ToggleButton btnMain;
    @BindView(R.id.btnTask)
    ToggleButton btnTask;
    @BindView(R.id.btnReport)
    ToggleButton btnReport;
    @BindView(R.id.btnMy)
    ToggleButton btnMy;
    public FragmentManager fragmentmanager;
    private MainFragment mainFragment;
    private TaskFragment taskFragment;
    private ReportFragment reportFragment;
    private MyFragment myFragment;
    MyApplication app;

    String latitude = "";
    String longitude = "";
    String userid;
    String username;
    String ssgrid;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2, Config.toptitle, -1);
        Intent intent = new Intent();
        intent.setClass(Main2Activity.this, NotifyService.class);
        startService(intent);
        //NotificationService
        Intent intent1 = new Intent();
        intent1.setClass(Main2Activity.this, NotificationService.class);
        startService(intent1);

        app = (MyApplication) getApplication();
        app.addActivityList(Main2Activity.this);
//        app.addActivity(this);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initview();

//        addPersonpos();
    }

    private void initview() {
        AppPreferences appPreferences = new AppPreferences(Main2Activity.this);
        userid = appPreferences.getString("userid", "");
        username = appPreferences.getString("name", "");
        ssgrid =appPreferences.getString("ssgrid", "");
        fragmentmanager = getFragmentManager();
        setDefaultFragment();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onListSyncEvent(MainSyncEvent event){
        initview();
    }


    public void setDefaultFragment() {
        FragmentTransaction transaction = fragmentmanager.beginTransaction();
        mainFragment = new MainFragment();
        transaction.replace(R.id.contents, mainFragment);
        transaction.commit();

    }

    @OnClick(R.id.btnMain)
    public void onBtnMainClicked() {
        btnMain.setChecked(true);
        btnTask.setChecked(false);
        btnReport.setChecked(false);
        btnMy.setChecked(false);
        android.app.FragmentTransaction transaction = fragmentmanager.beginTransaction();
        if(mainFragment==null){
            mainFragment = new MainFragment();
        }
        transaction.replace(R.id.contents, mainFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @OnClick(R.id.btnTask)
    public void onBtnTaskClicked() {
        btnMain.setChecked(false);
        btnTask.setChecked(true);
        btnReport.setChecked(false);
        btnMy.setChecked(false);
        android.app.FragmentTransaction transaction = fragmentmanager.beginTransaction();
        if(taskFragment==null){
            taskFragment = new TaskFragment();
        }
        transaction.replace(R.id.contents, taskFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @OnClick(R.id.btnReport)
    public void onBtnReportClicked() {
        btnMain.setChecked(false);
        btnTask.setChecked(false);
        btnReport.setChecked(true);
        btnMy.setChecked(false);
        android.app.FragmentTransaction transaction = fragmentmanager.beginTransaction();
        if(reportFragment==null){
            reportFragment = new ReportFragment();
        }
        transaction.replace(R.id.contents, reportFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @OnClick(R.id.btnMy)
    public void onBtnMyClicked() {
        btnMain.setChecked(false);
        btnTask.setChecked(false);
        btnReport.setChecked(false);
        btnMy.setChecked(true);
        android.app.FragmentTransaction transaction = fragmentmanager.beginTransaction();
        if(myFragment==null){
            myFragment = new MyFragment();
        }
        transaction.replace(R.id.contents, myFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_logout) {
           /* AppPreferences appPreferences = new AppPreferences(this);
            appPreferences.remove("permission");
            appPreferences.clear();
            Intent i = new Intent(this, LoginActivity.class);
            startActivity(i);*/
            app.exit();
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onFragmentInteraction(Uri uri) {

    }


}
